/**
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.

 Created on Nov 11, 2013 10:43:16 AM
 */
package com.googlecode.backtest.dataset.provider;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipInputStream;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import com.google.srclib.data.DurationType;
import com.google.srclib.dataset.SimpleResultSet;
import com.google.srclib.dataset.StockRecordDataset;
import com.google.srclib.io.zip.Zipper;
import com.googlecode.backtest.dataset.BackTestStockDataset;
import com.googlecode.backtest.dataset.IBackTestStockDataset;

/**
 * @author lianyuy
 * 
 */
@XmlRootElement(name = "BackTestStockDataProvider")
@XmlType(name = "BackTestStockDataProviderType")
public class BackTestStockDataProvider extends DataProvider {
	@XmlAttribute
	protected String filename;

	public BackTestStockDataProvider() {
		super();
	}

	/**
	 * @param filename
	 * @param stocks
	 */
	public BackTestStockDataProvider(String filename, ArrayList<String> stocks) {
		this();
		this.filename = filename;
		this.stocks = stocks;
	}

	/**
	 * @return the filename
	 */
	public String getFilename() {
		return filename;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.document.IDataProvider#getDataset()
	 */
	// @Override
	public Map<String, IBackTestStockDataset> getDataset() {
		Map<String, IBackTestStockDataset> map = null;
		if (stocks == null)
			return map;
		else {
			Zipper zipper = Zipper.getInstance();
			ZipInputStream zin = zipper.getZipFile(filename);
			try {
				map = new HashMap<String, IBackTestStockDataset>();
				for (String stock : stocks) {
					SimpleResultSet srs = (SimpleResultSet) zipper.csvReader(
							zin, stock + ".csv");
					if (srs != null) {
						StockRecordDataset sd = new StockRecordDataset(srs);
						map.put(stock, new BackTestStockDataset(sd,
								DurationType.DAYS));
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return map;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.backtest.document.IDataProvider#getDataset(java.lang.String
	 * )
	 */
	@Override
	public IBackTestStockDataset getDataset(String item) {
		Zipper zipper = Zipper.getInstance();
		ZipInputStream zin = zipper.getZipFile(filename);
		try {
			SimpleResultSet srs = (SimpleResultSet) zipper.csvReader(zin, item
					+ ".csv");
			if (srs != null) {
				StockRecordDataset sd = new StockRecordDataset(srs);
				return new BackTestStockDataset(sd, DurationType.DAYS);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
